package com.vicene.gmail.search.multhreads;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(Thread.currentThread().getName() + "......");

        //Runnable
        new Thread(() -> System.out.println(Thread.currentThread().getName() + ":启动")).start();

        //Thread
        new TestThread().start();


        //Callable FutureTask
        //有返回值
        FutureTask<Integer> futureTask = new FutureTask<>(new Call());
        new Thread(futureTask).start();;
        System.out.println(futureTask.get());

        //ThreadPool
        ExecutorService service = Executors.newFixedThreadPool(10);
        List<Thing> list= new ArrayList<>();
        for(int i = 0 ; i < 10 ; i++){
            list.add(new Thing());
        }

        list.stream().forEach( item -> service.execute(item));
        ;
    }


}
class Thing implements Runnable{

    @Override
    public void run() {
        for(int i = 0 ; i < 10 ; i++){
            System.out.println(Thread.currentThread().getName() + "_" +i);
        }
    }
}

class TestThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + ":启动");

    }
}

class Call implements Callable{

    @Override
    public Object call() throws Exception {
        int i;
        for( i = 0 ; i < 10 ; i++){
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
        return i;
    }
}